import { createRouter, createWebHashHistory } from 'vue-router'
//引入store
import store from '../store/index';
const routes = [
  {
    path: '/',
    name: 'home',
    component: () => import('../views/Home.vue'),
    meta: {
      auth:true,
      authority:0,
    },
    children:[
      {
        path:'',
        name:'Content',
        component: () => import('../views/Content.vue'),
        meta: {
          auth:true,
          authority:2,
          title: '首页'
        }
      },{
        path:'/Classinformation',
        name:'Classinformation',
        component: () => import('../views/Classinformation.vue'),
        meta: {
          auth:true,
          authority:3,
          title: '班级管理'
        }
      },{
        path:'/classroom',
        name:'课堂记录',
        component: () => import('../views/classroom.vue'),
        meta: {
          auth:true,
          authority:2,
          title: '课堂记录'
        }
      },{
        path:'/Studentsearch',
        name:'学生查询',
        component: () => import('../views/Studentsearch.vue'),
        meta: {
          auth:true,
          authority:2,
          title: '学生查询'
        }
      },{
        path:'/examination',
        name:'上传试卷',
        component: () => import('../views/examination.vue'),
        meta: {
          auth:true,
          authority:2,
          title: '上传试卷'
        }
      },{
        path:'/generate',
        name:'试卷生成',
        component: () => import('../views/generate.vue'),
        meta: {
          auth:true,
          authority:2,
          title: '试卷生成'
        }
      },{
        path:'/questions/:id',
        name:'试题大全',
        component: () => import('../views/questions.vue'),
        meta: {
          auth:true,
          authority:2,
          title: '试题大全'
        }
      },{
        path:'/Startthe_exam',
        name:'考试',
        component: () => import('../views/Startthe_exam.vue'),
        meta: {
          auth:true,
          authority:1,
          title: '考试'
        }
      },{
        path:'/TiMData',
        name:'题库',
        component: () => import('../views/TiMData.vue'),
        meta: {
          auth:true,
          authority:1,
          title: '题库'
        }
      },{
        path:'/history',
        name:'历史成绩',
        component: () => import('../views/history.vue'),
        meta: {
          auth:true,
          authority:1,
          title: '历史成绩'
        }
      },{
        path:'/student',
        name:'学员录入',
        component: () => import('../views/student.vue'),
        meta: {
          auth:true,
          authority:3,
          title: '学员录入'
        }
      },{
        path:'/Personal',
        name:'个人中心',
        component: () => import('../views/Personal.vue'),
        meta: {
          auth:true,
          authority:1,
          title: '个人中心'
        }
      }
      ,{
        path:'/ScoreStatistics',
        name:'成绩统计',
        component: () => import('../views/ScoreStatistics.vue'),
        meta: {
          auth:true,
          authority:1,
          title: '成绩统计'
        }
      }
    ]
  },{
    path:'/Login',
    name:'登录',
    component: () => import('../views/Login.vue'),
  }
]
const router = createRouter({
  history: createWebHashHistory(),
  routes
})
// 设置路由守卫
router.beforeEach((to,from,next)=>{
// name：wanghaiqi
// Time：2024-04-09 
// 是否跳转到权限不够页面，可以在下边追加一个判断-----
// next()
  if(to.matched.some(route=>route.meta.auth)){
    if(JSON.parse(sessionStorage.getItem('info')) == null){
      console.log('123456798')
      // next('/Login')
      next()
    }else{
      store.state.userInfor.authority = JSON.parse(sessionStorage.getItem('info')).r_id
      store.state.userInfor.name = JSON.parse(sessionStorage.getItem('info')).u_name
      store.state.userInfor.phone = JSON.parse(sessionStorage.getItem('info')).u_birth
      store.state.userInfor.r_name = JSON.parse(sessionStorage.getItem('info')).r_name
      store.state.userInfor.c_id = JSON.parse(sessionStorage.getItem('info')).c_id
      if(JSON.parse(sessionStorage.getItem('info')).r_id >= to.matched[1].meta.authority){
        next()
      }
    }
  }else{
    next()
  }
})
export default router